package com.wlyuan.open.utils;

import cn.hutool.core.util.DesensitizedUtil;
import com.wlyuan.core.marker.NamedStatus;
import com.wlyuan.open.domain.client.OpenClient;
import com.wlyuan.open.remote.facade.Tenant;
import com.wlyuan.open.remote.facade.User;
import com.wlyuan.open.vo.OpenClientApiVO;
import com.wlyuan.open.vo.OpenClientDetailVO;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Optional;

/**
 * @author lijing
 */
public class OpenClientDetailBuilder {
    private final OpenClientDetailVO client;

    public OpenClientDetailBuilder(OpenClient openClient) {
        this.client = new OpenClientDetailVO();
        BeanUtils.copyProperties(openClient, client);
    }

    public OpenClientDetailBuilder(Tenant source) {
        this.client = new OpenClientDetailVO();
        BeanUtils.copyProperties(source, client);
    }

    public OpenClientDetailBuilder withClient(OpenClient client) {
        this.client.setAppId(client.getAppId());
        this.client.setName(client.getName());
        this.client.setHasEnable(client.hasEnable());
        this.client.setHasBind(client.bind());
        this.client.setHasAuditing(client.hasAuditing());
        this.client.setHasReject(client.hasReject());
        this.client.setHasDisable(client.hasDisable());
        Optional.ofNullable(client.getTenantId()).ifPresent(t -> {
            this.client.setTenantId(t.getId());
        });
        Optional.ofNullable(client.getId()).ifPresent(i -> {
            this.client.setClientId(i.getId());
        });
        Optional.ofNullable(client.getUserId()).ifPresent(u -> {
            this.client.setUserId(u.getId());
        });
        Optional.ofNullable(client.getStatus()).ifPresent(s -> {
            this.client.setStatus(s.getValue());
            this.client.setStatusName(s.getName());
        });
        return this;
    }

    public OpenClientDetailBuilder withUser(User user) {
        Optional.ofNullable(user).ifPresent(u -> {
            this.client.setAccount(u.getAccount());
            if (BooleanUtils.isTrue(u.getIsAlias())) {
                this.client.setAccount(u.getAccount().concat("@").concat(u.getAlias()));
            }
            this.client.setAccountStatus(u.getStatus());
            this.client.setAccountStatusName(u.getStatusName());
            this.client.setAccountEnable(NamedStatus.ENABLED.getValue().equals(u.getStatus()));
        });
        return this;
    }

    public OpenClientDetailBuilder withApiList(List<OpenClientApiVO> list) {
        this.client.setApiList(list);
        return this;
    }

    public OpenClientDetailBuilder noWithAppSecret() {
        this.client.setAppSecret("");
        return this;
    }

    public OpenClientDetailBuilder desensitizedMobile() {
        this.client.setMobile(DesensitizedUtil.mobilePhone(this.client.getMobile()));
        return this;
    }

    public OpenClientDetailVO build() {
        if (null == client.getUserId()) {
            client.setUserId(0L);
        }
        return client;
    }
}
